package com.cn.delan.web.mvc.admin.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.jar.JarInputStream;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipEntry;

import com.cn.delan.core.util.DateUtil;
import com.cn.delan.core.util.FileUtil;
import com.cn.delan.web.mvc.admin.AdminConst;
import com.cn.delan.web.mvc.admin.service.IUnZipService;

public class UnZipService extends Thread implements IUnZipService {
	private static Hashtable<String,UnZipService> unziping = new Hashtable<String,UnZipService>();
	private String path;
	//完成zip的百分比
	private long percent;
	private File file;
	private File unZipDir;
	//要压缩的文件总大小
	private long totalSize;
	//压缩了的文件大小
	private long unZipSize;
	//message为非空字符串时表示zip报错
	private String message = "";
	
	public UnZipService(){
		
	}
	private UnZipService(String path){
		this.path = path;
		this.percent = 0;
	}
	@Override
	synchronized public IUnZipService unzip(String path) {
		UnZipService service = UnZipService.unziping.get(path);
		if(service==null){
			this.file = new File(path);
			File parentFile = file.getParentFile();
			this.unZipDir = new File(parentFile,file.getName().substring(0,file.getName().length()-4));
			service = new UnZipService(path);
			service.setFile(this.file);
			service.setUnZipDir(this.unZipDir);
			service.setTotalSize(FileUtil.size(this.file));
			if(file.exists()){
				if(unZipDir.exists()){
					//service.setMessage(zipFile.getAbsolutePath()+"{"+AdminConst.ERROR_0002+"}");
					//文件夹已存在，代表解包操作已完成。
					service.setMessage("");
					service.setPercent(100);
				}else{
					UnZipService.unziping.put(path, service);
					service.start();
				}
			}else{
				service.setMessage(file.getAbsolutePath()+"{"+AdminConst.MESSAGE_FILE_NOTEXISTS+"}");
			}
		}
		return service;
	}
	@Override
	public void run() {
		try{
			this.unzip(this.file,this.unZipDir);
		}catch(IOException e){
			this.message = e.getMessage();
		}finally{
			UnZipService.unziping.remove(path);
		}
	}
	private void unzip(File file,File unZipDir) throws IOException{
		if(!file.exists()){
			return;
		}
		FileInputStream fis = new FileInputStream(file);
		JarInputStream jis = new JarInputStream(fis);
		ZipEntry entry = null;
		while((entry=jis.getNextEntry())!=null){
			byte[] b = new byte[2000];
			int len = -1;
			File unzip = new File(unZipDir,entry.getName());
			if(entry.isDirectory()){
				unzip.mkdirs();
				continue;
			}
			FileOutputStream fos = new FileOutputStream(unzip);
			while((len=jis.read(b))!=-1){
				fos.write(b, 0, len);
				this.unZipSize+=len;
				this.percent=this.unZipSize*100/this.totalSize;
			}
			fos.close();
		}
	}
	public long getPercent() {
		return this.percent;
	}
	public long getTotalSize() {
		return this.totalSize;
	}
	public String getMessage(){
		return this.message;
	}
	private void setTotalSize(long totalSize){
		this.totalSize = totalSize;
	}
	private void setFile(File file) {
		this.file = file;
	}
	private void setUnZipDir(File unZipDir) {
		this.unZipDir = unZipDir;
	}
	private void setMessage(String message) {
		this.message = message;
	}
	private void setPercent(long percent) {
		this.percent = percent;
	}
}
